package com.woniu.user.controller;

import com.woniu.annotation.RequirePermission;
import com.woniu.annotation.RequireRole;
import com.woniu.dto.AreaCodeDTO;
import com.woniu.user.service.AreaCodeService;
import com.woniu.utils.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 区域表 前端控制器
 * </p>
 *
 * @author ly
 * @since 2024年12月05日
 */
@RestController
@RequestMapping("/areaCode")
@Api(tags = "区域管理")
public class AreaCodeController {

    @Autowired
    private AreaCodeService areaCodeService;

    @PostMapping("/addAreaCode")
    @ApiOperation("添加区域接口")
    @RequireRole("管理员")
    @RequirePermission("添加区域")
    public Result addAreaCode(@ApiParam("区域名称") String areaCode) throws Exception {
        Integer result = areaCodeService.addAreaCode(areaCode);
        if (result == 1) {
            return Result.getResult().setMsg("添加成功").setCode(200);
        } else {
            return Result.getResult().setMsg("添加失败").setCode(500);
        }
    }


    /**
     * 查询区域接口
     *
     * @return Result
     */
    @GetMapping("/listAreaCode")
    @ApiOperation("查询区域列表接口")
    public Result list() throws Exception {
        List<AreaCodeDTO> areaCodes = areaCodeService.listAreaCode();
        return Result.getResult(List.class).setData(areaCodes).setCode(200);
    }

    @GetMapping("/getAreaCodeById")
    @ApiOperation("根据id查询区域接口")
    public Result getAreaCodeById(@ApiParam("区域id") @RequestParam("areaCodeId") Integer areaCodeId) throws Exception {
        String areaCodeName = areaCodeService.getAreaCodeById(areaCodeId);
        return Result.getResult(String.class).setData(areaCodeName).setCode(200);
    }

    /**
     * 删除区域接口
     *
     * @param areaCodeId 区域id
     * @return Result
     */
    @DeleteMapping("/deleteAreaCode")
    @ApiOperation("删除区域接口")
    @RequireRole("管理员")
    @RequirePermission("删除区域")
    public Result deleteAreaCode(@ApiParam("区域id") @RequestParam("areaCodeId") Integer areaCodeId) throws Exception {
        Integer result = areaCodeService.deleteAreaCode(areaCodeId);
        if (result == 1) {
            return Result.getResult().setMsg("删除成功").setCode(200);
        } else {
            return Result.getResult().setMsg("删除失败").setCode(500);
        }
    }
}
